package com.lingtao.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.permissionx.guolindev.PermissionX;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("native-lib");
//        System.loadLibrary("ijkffmpeg");
//        System.loadLibrary("ijksdl");
//        System.loadLibrary("ijkplayer");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.toPlay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionX.init(MainActivity.this)
                        .permissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .onExplainRequestReason((scope, deniedList) -> {
                            String message = getString(R.string.app_name) + "需要您同意以下权限才能正常使用";
                            scope.showRequestReasonDialog(deniedList, message, "确定", "取消");
                        })
                        .request((allGranted, grantedList, deniedList) -> {
                            if (allGranted) {
                                PlayVideoActivity.start(MainActivity.this);
                            } else {
                                Toast.makeText(MainActivity.this, "没有权限", Toast.LENGTH_LONG).show();
                            }
                        });
            }
        });
        findViewById(R.id.recordBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "临时存储.mp4";
                File file = new File(path);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                recordVideo(path);
            }
        });
    }

    public native String stringFromJNI();

    public native void recordVideo(String path);
}
